package com.educational.Controller;

import com.educational.Entity.Admin;
import com.educational.Service.AdminService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Admin控制类
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    /**
     * 依赖注入
     */
    @Autowired
    private AdminService adminService;

    /**
     *
     */
    @RequestMapping("/tologin")
    public String toLogin(){
        return "login";
    }

    /**
     * 通过账号密码来验证管理员登录信息
     */
    @RequestMapping("/login")
    public String AdminLogin(@Param("admin_actnumber") String admin_actnumber, @Param("admin_password") String admin_password, Model model, HttpSession session){
        Admin admin = adminService.AdminLogin(admin_actnumber, admin_password);
        if(admin != null){
            if (admin.getAdmin_password().equals(admin_password)){
                System.out.println("密码正确，登录成功！");
                session.setAttribute("AdminLoginSession",admin);
                return "main";
            }else{
                System.out.println("密码错误，登陆失败！");
                model.addAttribute("error","密码错误，登陆失败！");
                return "login";
            }
        }else{
            System.out.println("用户不存在，请重新输入！");
            model.addAttribute("error","用户不存在，请重新输入！");
            return "login";
        }
    }

}
